package com.ocom.company.controller;


import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.ocom.common.constant.CompanyServiceUrl;
import com.ocom.common.entity.human.EntityBizLeaveApply;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.human.LeaveApplyDetailRequest;
import com.ocom.common.request.human.NewLeaveApplyRequest;
import com.ocom.common.request.human.UpdateLeaveApplyRequest;
import com.ocom.common.request.ugcapi.GzhUserLeaveList;
import com.ocom.common.request.web.CommonPidVo;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.CommonUtil;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.human.LeaveApplyDetailVo;
import com.ocom.company.service.BizLeaveApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@RestController
public class LeaveApplyController {


    @Autowired
    BizLeaveApplyService bizLeaveApplyService;

    @PostMapping(CompanyServiceUrl.URL_leavelApplyOne)
    public Result leavelApplyOne(@RequestBody EntityBizLeaveApply entityBizLeaveApply){
        return ResultUtil.success(bizLeaveApplyService.getApplyOne(entityBizLeaveApply.getComId(),entityBizLeaveApply));
    }

    @PostMapping(CompanyServiceUrl.URL_leavelApplyList)
    public Result leavelApplyList(@RequestBody GzhUserLeaveList request){
        if(CommonUtil.isNull(request.getDefKey())) request.setDefKey("leave-apply"+"-"+request.getComId());
        return ResultUtil.success(bizLeaveApplyService.getApplyList(request.getComId(),request));
    }


    @PostMapping(CompanyServiceUrl.URL_addLeavelApply)
    public Result addLeavelApply(@RequestBody EntityBizLeaveApply entityBizLeaveApply){
        if(CommonUtil.isNull(entityBizLeaveApply.getDefKey())) entityBizLeaveApply.setDefKey("leave-apply"+"-"+entityBizLeaveApply.getComId());
        EntityBizLeaveApply entityBizLeaveApply1 = bizLeaveApplyService.addApply(entityBizLeaveApply.getComId(),entityBizLeaveApply);
        if(entityBizLeaveApply1==null)  return   ResultUtil.error(ResultEnum.Err_2009);
        return ResultUtil.success(entityBizLeaveApply);
    }


    @PostMapping(CompanyServiceUrl.URL_updateLeavelApply)
    public Result updateLeavelApply(@RequestBody EntityBizLeaveApply entityBizLeaveApply){
        boolean b = bizLeaveApplyService.updateApply(entityBizLeaveApply.getComId(),entityBizLeaveApply);
        if(!b )  return   ResultUtil.error(ResultEnum.Err_3026);
        return ResultUtil.success();
    }


    @PostMapping(CompanyServiceUrl.URL_queryUserLeave)
    public Result queryUserLeave(@RequestBody CommonPidVo commonPidVo) {
        Long comId = commonPidVo.getComId() ;
        Long pId = commonPidVo.getPId();
        Integer leaveCount = bizLeaveApplyService.queryUserLeave(comId, pId);
        if (leaveCount != null && leaveCount > 0) {
            return ResultUtil.success();
        }
        return ResultUtil.error(-2);
    }

    @PostMapping(CompanyServiceUrl.URL_newLeaveApply)
    public Result<EntityBizLeaveApply> newLeaveApply(@RequestBody NewLeaveApplyRequest leaveApplyRequest){
        EntityBizLeaveApply entityBizLeaveApply = Convert.convert(EntityBizLeaveApply.class,leaveApplyRequest);
        if(CommonUtil.isNull(entityBizLeaveApply.getDefKey())) entityBizLeaveApply.setDefKey("leave-apply"+"-"+entityBizLeaveApply.getComId());
        bizLeaveApplyService.save(entityBizLeaveApply.getComId(),entityBizLeaveApply);
        return ResultUtil.success(entityBizLeaveApply);
    }

    @PostMapping(CompanyServiceUrl.URL_updateLeaveApply)
    public Result updateLeaveApply(@RequestBody UpdateLeaveApplyRequest leaveApplyDetailRequest){
        EntityBizLeaveApply entityBizLeaveApply = Convert.convert(EntityBizLeaveApply.class,leaveApplyDetailRequest);
        this.bizLeaveApplyService.update(entityBizLeaveApply.getComId(),entityBizLeaveApply);
        return ResultUtil.success();
    }

    @PostMapping(CompanyServiceUrl.URL_leaveApplyDetail)
    public Result leaveApplyDetail(@RequestBody LeaveApplyDetailRequest leaveApplyDetailRequest){
        EntityBizLeaveApply entityBizLeaveApply = Convert.convert(EntityBizLeaveApply.class,leaveApplyDetailRequest);
        entityBizLeaveApply = this.bizLeaveApplyService.getApplyOne(leaveApplyDetailRequest.getComId(),entityBizLeaveApply);
        if(ObjectUtil.isEmpty(entityBizLeaveApply)){
            return ResultUtil.success();
        }
        LeaveApplyDetailVo leaveApplyDetailVo = new LeaveApplyDetailVo();

        LeaveApplyDetailVo.RawData rawData = Convert.convert(LeaveApplyDetailVo.RawData.class,entityBizLeaveApply);
        LeaveApplyDetailVo.Param param = Convert.convert(LeaveApplyDetailVo.Param.class,entityBizLeaveApply);
        List<LeaveApplyDetailVo.FormatData> formatDataList = new ArrayList<>();

        leaveApplyDetailVo.setRawData(rawData);
        leaveApplyDetailVo.setParam(param);
        if(entityBizLeaveApply.getDays().compareTo(BigDecimal.ZERO) > 0 && entityBizLeaveApply.getType()>=1 && entityBizLeaveApply.getType() <=5){
            LeaveApplyDetailVo.FormatData formatData = new LeaveApplyDetailVo.FormatData();
            formatData.setContent(entityBizLeaveApply.getDays().toString());
            formatData.setTitle("请假天数");
            formatData.setRowType(1);
            formatData.setReason(entityBizLeaveApply.getReason());
            formatDataList.add(formatData);
            leaveApplyDetailVo.setFormatData(formatDataList);
        }

        return ResultUtil.success(leaveApplyDetailVo);
    }

}
